<?php

namespace app\common\services;

use  yii\helpers\Html;

/**
 * Class UtilService 公共方法
 * Author: yanhaoqiang
 * @package app\common\services
 */
class UtilService {

    /**
     * [getRootPath 根路径]
     * @return string
     */
    public static function getRootPath() {
        $vendor_path = \Yii::$app->vendorPath;
        return dirname($vendor_path);
    }

    /**
     * [encode 字符串编码]
     * @param $dispaly_text
     * @return string
     */
    public static function encode($dispaly_text) {
        return Html::encode($dispaly_text);
    }

    /**
     * [getIP 用户 ip]
     * @return string
     */
    public static function getIP() {
        if (!empty($_SERVER["HTTP_X_FORWARDED_FOR"])) {
            return $_SERVER["HTTP_X_FORWARDED_FOR"];
        }
        return isset($_SERVER["REMOTE_ADDR"]) ? $_SERVER["REMOTE_ADDR"] : '';
    }

    /**
     * [isWechat 是否为微信登录]
     * @return bool
     */
    public static function isWechat() {
        $ug = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
        if (stripos($ug, 'micromessenger') !== false) {
            return true;
        }
        return false;
    }
}